package com.gvtv.main.params;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.gvtv.main.util.AppTools;

public class ParamsFactory {
	
	private static final Logger logger = Logger.getLogger(ParamsFactory.class);
	
	private static Map<String, ParamsConvert> paramsMap;
	private static String PARAMS_REGIST = "/conf/params-regist.xml";
	private static byte[] lock = new byte[0];
	private static ParamsFactory instance;
	
	public ParamsFactory(){
		paramsMap = new ConcurrentHashMap<String, ParamsConvert>();
		init();
	}
	public static ParamsFactory getInstance() {
		if (instance != null) {
			return instance;
		} else {
			synchronized (lock) {
				if (instance != null) {
					return instance;
				} else {
					instance = new ParamsFactory();
					return instance;
				}
			}
		}
	}
	private void init(){
		try {
			XMLConfiguration conf = new XMLConfiguration(AppTools.getBaseDir()+PARAMS_REGIST);
			List list = conf.getList("param.name");
			for(int i=0,len=list.size();i<len;i++){
				String name = conf.getString("param("+i+").name");
				String paramClass = conf.getString("param("+i+").param-class");
				registParams(name, (ParamsConvert)Class.forName(paramClass).newInstance());
			}
		} catch (ConfigurationException e) {
			logger.error("加载"+PARAMS_REGIST+"文件出错." +e);
		} catch (InstantiationException e) {
			logger.error(e);
		} catch (IllegalAccessException e) {
			logger.error(e);
		} catch (ClassNotFoundException e) {
			logger.error("加载"+PARAMS_REGIST+"文件中的class出错. "+e);
		}
	}
	private void registParams(String name, ParamsConvert paramsConvert){
		if(StringUtils.isBlank(name)){
			logger.error("名称为空.");
			return ;
		}
		if(paramsMap.containsKey(name)){
			logger.warn(name+"名称已存在,删除后重新添加.");
			paramsMap.remove(name);
		}
		paramsMap.put(name, paramsConvert);
	}
	
	public ParamsConvert get(String name){
		if(StringUtils.isBlank(name)){
			return null;
		}
		return paramsMap.get(name);
	}
	
	public String toValue(String name){
		ParamsConvert param = this.get(name);
		if(param==null){
			logger.warn("根据"+name+"名称获取的ParamsConvert为空,直接返回"+name);
			return name;
		}
		return param.value();
	}
	
	public String transfor(String str){
		if(StringUtils.isBlank(str)){
			return "";
		}
		Matcher ma = AppTools.pa.matcher(str);
		StringBuffer buf = new StringBuffer();
		while (ma.find()) {
			MatchResult mr = ma.toMatchResult();
			ma.appendReplacement(buf,toValue(mr.group(1)));
		}
		ma.appendTail(buf);
		return buf.toString();
	}
	
	public static void main(String[] args) {
		String sql = getInstance().transfor("select count(*) from t1 where c1='${CurrentDateTimeSSS}'");
		System.out.println(sql);
		sql = getInstance().transfor("select count(*) from t2 where c2='${CurrentDateTimeSSS}'");
		System.out.println(sql);
	}
}
